package com.tencent.wxcloudrun.controller.AdminUser;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nimbusds.jose.JOSEException;
import com.tencent.wxcloudrun.utils.JwtToken;
import com.tencent.wxcloudrun.utils.ThreadLocalTool;
import com.tencent.wxcloudrun.model.AdminUser;
import com.tencent.wxcloudrun.response.R;
import com.tencent.wxcloudrun.service.AdminUserService;
import com.tencent.wxcloudrun.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

/**
 * ClassName: AdminUserController
 * Package: com.tencent.wxcloudrun.controller
 * Description:
 *
 * @Date: 2023/1/22 8:34 AM
 * @Author: wht
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminUserController {
    @Autowired
    AdminUserService adminUserService;

    @PostMapping("/login")
    public <T> R<T> login(@RequestBody @Valid LoginData loginData) throws NoSuchAlgorithmException, JOSEException {

        AdminUser adminUser = adminUserService.login(loginData);

//        设置token
        String token = JwtToken.getToken(adminUser.getAdminUserId());
        ThreadLocalTool.setToken(token);

        return R.successMsgData("登录成功！", adminUserService.getResAdminUser(adminUser));
    }

    @PutMapping("/addOneAdminUser")
    public <T> R<T> addOneAdminUser(@RequestBody AdminUser adminUser) throws NoSuchAlgorithmException {
        adminUser.setPassword(Tools.getInitPwd());
        boolean res = adminUserService.save(adminUser);

        if (res) {
            return R.AddSuccess();
        } else {
            return R.AddFail();
        }
    }

    @DeleteMapping("/delOneAdminUser")
    public <T> R<T> delOneAdminUser(@RequestParam(required = true, value = "id") Integer id) {
        boolean res = adminUserService.removeById(id);

        if (res) {
            return R.DelSuccess();
        } else {
            return R.DelFail();
        }
    }


    // 修改自己的密码 ----------------------
    @PatchMapping("/resetMyPwd")
    public <T> R<T> resetMyPwd(@RequestBody @Valid ResetMyPwdData resetMyPwdData) throws NoSuchAlgorithmException {
        Boolean res = adminUserService.resetMyPwd(resetMyPwdData);

        if (res) {
            return R.UpdateSuccess();
        } else {
            return R.UpdateFail();
        }
    }

    // 重置别人的密码---------------------
    @PatchMapping("/resetOnePwd")
    public <T> R<T> resetOnePwd(@RequestBody @Valid ResetOnePwdData resetOnePwdData) throws NoSuchAlgorithmException {
        Boolean res = adminUserService.resetOnePwd(resetOnePwdData);

        if (res) {
            return R.UpdateSuccess();
        } else {
            return R.UpdateFail();
        }
    }

    // 修改自己的用户信息---------------------
    @PatchMapping("/updateMyInfo")
    public <T> R<T> updateMyInfo(@RequestBody @Valid UpdateMyInfoData updateMyInfoData) {
        Boolean res = adminUserService.updateMyInfo(updateMyInfoData);

        if (res) {
            return R.UpdateSuccess();
        } else {
            return R.UpdateFail();
        }
    }

    // 修改别人的用户信息---------------------
    @PatchMapping("/updateOneInfo")
    public <T> R<T> updateOneInfo(@RequestBody @Valid UpdateOneInfoData updateOneInfoData) throws NoSuchAlgorithmException {
        Boolean res = adminUserService.updateOneInfo(updateOneInfoData);

        if (res) {
            return R.UpdateSuccess();
        } else {
            return R.UpdateFail();
        }
    }

    @GetMapping("/getOneAdminUserByToken")
    public <T> R<T> getOneAdminUserByToken() {
        AdminUser adminUser = ThreadLocalTool.getUser();
        return R.successData(adminUserService.getResAdminUser(adminUser));
    }


    @GetMapping("/getAdminUserList")
    public <T> R<T> getAdminUserList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        Page adminUserList = adminUserService.getAdminUserList(pageNum, pageSize);
        return R.successData(adminUserList);
    }


    @GetMapping("/getCosTempToken")
    public <T> R<T> getCosTempToken() {
        HashMap res = adminUserService.getCosTempToken();
        return R.successData(res);
    }


}
